<?php
    class AreaController extends GTL_Action {
         public function init() {
            parent::init();
            /* Initialize action controller here */
            $this->lng_id = '1';
        }
        public function indexAction() {
            $this->_helper->redirector('list', 'area','default');
        }
        public function viewAction() {
            if ($this->_getParam('area_id') && $this->_getParam('area_id') != '') {
                $area_id = $this->_getParam('area_id');
            } else {
                $this->_helper->redirector('list', 'area', 'default');
            }
            $areaModel = new Model_Area();
            $areaalldata = $areaModel->fetchEntryById($area_id);
            $this->view->areaalldata = $areaalldata;
        }
        public function listAction() {
            $errorMessage= Array();
            $successmessage = '';
            $where = '';
            $searchtype = '';
            $orderField = 'lang_id';
            $sort = 'asc';
            $areascount = array();

            /* Pagination Login */
            $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
            $currentPageReviews = 1;
            //PAGING_PER_PAGE
            if ($this->_getParam('page') && $this->_getParam('page') != '') {
                $currentPageReviews = $this->_getParam('page');
            }

            if ($this->_getParam('field') && $this->_getParam('field') != '') {
                $orderField = $this->_getParam('field');
            }

            if ($this->_getParam('sort') && $this->_getParam('sort') != '') {
                $sort = $this->_getParam('sort');
            }
            $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

            if ($this->_request->isPost()) {
                $postedData = $this->_request->getPost();
                //Multiple Delete Logic 
                if (isset($postedData['multiaction']) && ($postedData['multiaction'] != '')) {
                    $action = trim($postedData ['multiaction']);
                    if (isset($postedData['multicheck']) && count($postedData['multicheck']) > 0) {
                        $ids = implode(',', $_POST['multicheck']);
                        $result = $this->deleteAction($_POST['multicheck']);
                    } else {
                        $errorMessage[] = ' Please select atleast one checkbox! ';
                    }
                }
                /*search code here*/
                 
                
            }

            $areaModel = new Model_Area();
            $areasData = $areaModel->fetchLISTArea($where, $itemsPerPageReviews, $offset, $orderField, $sort);
            $areascount = $areaModel->fetchLISTArea($where);
            
            $paginator = new GTL_Paginator();
            $paginator->setItemsPerPage($itemsPerPageReviews);
            $paginator->setItemsTotal( count($areascount));//$areaModel->getAreaCount($where));
            $paginator->setCurrentPage($currentPageReviews);
            $link = $this->view->url(array('controller' => 'area', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
            $paginator->paginate($link);

            $this->view->paginator = $paginator;
            $this->view->areasData = $areasData;
            $this->view->errorMessage = $errorMessage;
            $this->view->successmessage = $successmessage;
            $this->view->statusArray = $areaModel->getStatusArray();
            $this->view->searchtype = $searchtype;
        }
        public function deleteAction($ids = Array()) {
            //Load area Model 
            $areaModel = new Model_Area();
            if ($this->_getParam('area_ref_id') && $this->_getParam('area_ref_id') != '') {
                $area_ref_id = $this->_getParam('area_ref_id');
                $areaModel->deleteArea($area_ref_id);
                $this->addSuccessMessage('Record Successfully Deleted');
               $this->_helper->redirector('list', 'area', 'default');
            } else {
                $areaModel->deleteArea($ids);
               $this->addSuccessMessage('Record Successfully Deleted');
                $this->_helper->redirector('list', 'area', 'default');
            }
        }
        public function formAction() {
     //Initialize
            $successmessage = '';
            $errorMessageArray = Array();
            $data = array();
            $areaData = Array();
            $this->view->mode = 'Add';

            //Load area Model
            $areaModel = new Model_Area();

            //Load area Form
            //$areaForm = new Form_AreaForm();

            $cityModel = new Model_City();
            $cityArray = $cityModel->fetchCity("city_lang_id = '".$this->lng_id."'");
            $languageModel = new Model_Language();
           
            /*$languageArray = $languageModel->fetchlanguageArray();
            $areaForm->area_lang_id->addMultiOptions($languageArray);
            
            $cityModel = new Model_City();
            $cityArray = $cityModel->fetchCity();
            $areaForm->city_id->addMultiOptions($cityArray);
            
            //Load the status array for the select box in form 
            $statusArray = $areaModel->getStatusArray();
            $areaForm->area_status->addMultiOptions($statusArray);
            */

            $languagesArray = $languageModel->fetchlanguages();
            $languages = Array();
            foreach ($languagesArray as $key => $_lan) {
                $languages[$_lan['lang_id']] = $_lan;
                $languages[$_lan['lang_id']] = $_lan;
                $languages[$_lan['lang_id']]['area_identity_code'] = "";
                $languages[$_lan['lang_id']]['area_name'] = "";
                $languages[$_lan['lang_id']]['area_title'] = "";
                $languages[$_lan['lang_id']]['city_id'] = "";
                $languages[$_lan['lang_id']]['area_ref_id'] = "";
                $languages[$_lan['lang_id']]['area_id'] = "";
                
            }
            
            
            //Check for the area Parameter
            $areaData['area_ref_id'] = $this->_request->getParam('area_ref_id');
            $area_ref_id = (!empty($areaData['area_ref_id'])) ? $areaData['area_ref_id'] : null;
            
            
            if (!empty($area_ref_id)) {
                //Set the Mode to Edit
                $this->view->mode = 'Edit';
                //Fetch data to be loaded in form 
                $data = $areaModel->fetchEntryByREFId($area_ref_id);
                foreach ($data as $_desc) {
                    if (array_key_exists($_desc['area_lang_id'], $languages)) {
                        $languages[$_desc['area_lang_id']]['area_identity_code'] = $_desc['area_identity_code'];
                        $languages[$_desc['area_lang_id']]['area_name'] = $_desc['area_name'];
                        $languages[$_desc['area_lang_id']]['area_title'] = $_desc['area_title'];
                        $languages[$_desc['area_lang_id']]['city_id'] = $_desc['city_id'];
                        $languages[$_desc['area_lang_id']]['area_id'] = $_desc['area_id'];
                        $languages[$_desc['area_lang_id']]['area_ref_id'] = $area_ref_id;
                        
                        
                    }
                }
            }
            //Populate form 
           
            if ($this->getRequest()->isPost()) {
                //Check for Errors
                $postedData = $this->_request->getPost();
                
                /* if($postedData['area_identity_code']== ''){
                    $errorMessageArray[] = 'Please Enter identity code';
                }
                if($postedData['area_name']== ''){
                    $errorMessageArray[] = 'Please enter name';
                }*/
                if($postedData['city_id']== '' ){
                    $errorMessageArray[] = 'Please select city';
                }
              /*  $languages[1]['area_identity_code'] = $postedData['area_identity_code'];
                $languages[1]['area_name'] = $postedData['area_name'];
              */  $languages[1]['city_id'] = $postedData['city_id'];
                
                
                
                if($postedData['1_area_title']== ''){
                    $errorMessageArray[] = 'English value should not be blank.';
                    foreach ($languages as $key => $_lang) {
                       /* $languages[$key]['area_identity_code'] = $postedData['area_identity_code'];
                        $languages[$key]['area_name'] = $postedData['area_name'];
                      */  $languages[$key]['area_title'] = $postedData[$_lang['lang_id'].'_area_title'];
                        $languages[$key]['city_id'] = $postedData['city_id'];
                        
                    }
                }        
                    if (count($errorMessageArray) > 0) {

                    } else {
                    
                          
                       /* if ($this->view->mode == 'Add') {
                            $areaData['area_createddate'] = date('Y-m-d H:i:s');
                        }
                        //area save
                        $areaData['area_id'] = $area_id;
                        $areaData['area_identity_code'] = $postedData['area_identity_code'];
                        $areaData['area_name'] = $postedData['area_name'];
                        $areaData['area_title'] = $postedData['area_title'];
                        $areaData['area_lang_id'] = $postedData['area_lang_id'];
                        $areaData['city_id'] = $postedData['city_id'];
                        $areaData['area_updateddate'] = date('Y-m-d H:i:s');
                        $areaData['area_status'] = $postedData['area_status'];
                        $areaModel->save($areaData, 'area_id');
                        * 
                        */
                        if($this->view->mode == 'Add'){
                        foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $english_code = $postedData[$_lang['lang_id'].'_area_title'];
                                $areaData['area_identity_code'] = strtolower($english_code);
                                $areaData['area_name'] = strtolower($english_code);
                                $areaData['area_title'] = $postedData[$_lang['lang_id'].'_area_title'];
                                $areaData['city_id'] = $postedData['city_id'];
                                $areaData['area_createddate'] = date('Y-m-d H:i:s');
                                $areaData['area_updateddate'] = date('Y-m-d H:i:s');
                                $areaData['area_lang_id'] = $_lang['lang_id'];
                                $areaData['area_status'] = 1;
                                $areaData['area_ref_id'] = $_lang['area_ref_id'];
                                $last_id = $areaModel->save($areaData, 'area_id');
                                $areaData1['area_id'] = $last_id;
                                $areaData1['area_ref_id'] = $last_id;
                                $areaModel->save($areaData1, 'area_id');
                            }else{
                                $areaData['area_identity_code'] = strtolower($english_code);
                                $areaData['area_name'] = strtolower($english_code);
                                $areaData['area_title'] = $postedData[$_lang['lang_id'].'_area_title'];
                                $areaData['city_id'] = $postedData['city_id'];
                                $areaData['area_createddate'] = date('Y-m-d H:i:s');
                                $areaData['area_updateddate'] = date('Y-m-d H:i:s');
                                $areaData['area_lang_id'] = $_lang['lang_id'];
                                $areaData['area_status'] = 1;
                                $areaData['area_ref_id'] = $last_id;
                                if($areaData['area_name']!= '' ){
                                    $areaModel->save($areaData, 'area_id');
                                }
                            }
                        }
                   }
                         else{
                       //edit mode
                       foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $english_code = $postedData[$_lang['lang_id'].'_area_title'];
                                $areaData['area_id'] = $_lang['area_id'];
                                $areaData['area_identity_code'] = strtolower($english_code);
                                $areaData['area_name'] = strtolower($english_code);
                                $areaData['area_title'] = $postedData[$_lang['lang_id'].'_area_title'];
                                $areaData['city_id'] = $postedData['city_id'];
                                $areaData['area_lang_id'] = $_lang['lang_id'];
                                $areaData['area_status'] = 1;
                                $areaData['area_updateddate'] = date('Y-m-d H:i:s');
                                $areaData['area_ref_id'] = $area_ref_id;
                                $areaModel->save($areaData, 'area_id');
                                
                            }else{
                                $areaData['area_id'] = $_lang['area_id'];
                                $areaData['area_identity_code'] = strtolower($english_code);
                                $areaData['area_name'] = strtolower($english_code);
                                $areaData['area_title'] = $postedData[$_lang['lang_id'].'_area_title'];
                                $areaData['city_id'] = $postedData['city_id'];
                                $areaData['area_lang_id'] = $_lang['lang_id'];
                                $areaData['area_status'] = 1;
                                $areaData['area_updateddate'] = date('Y-m-d H:i:s');
                                $areaData['area_ref_id'] = $area_ref_id;
                                $areaModel->save($areaData, 'area_id');
                                
                            }
                        }
                     
                   }
                        
                        if (count($errorMessageArray) > 0) {

                        } else {
                            if ($this->view->mode == 'Add') {
                                $successmessage = 'Area add successfully';
                            } else {
                                $successmessage = 'Area updated successfully';
                            }
                            $this->addSuccessMessage($successmessage);
                            $this->_helper->redirector('list', 'area', 'default');
                        }
                    }
                
            }
            $this->view->cityArray = $cityArray;
            $this->view->languages = $languages;
            $this->view->areaid = $area_ref_id;
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
           // $this->view->form = $areaForm;


    }
    }?>